/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yutel.gui;

import com.yutel.biz.Login;
import com.yutel.main.WSClientMIDlet;
import com.yutel.util.LangUtil;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author yuanyajun
 */
public class LoginForm extends Form implements CommandListener, Runnable {

    private TextField username;
    private TextField password;
    private ChoiceGroup autoLogin;
    private Command loginCom;
    private Command registerCom;
    private Thread t;

    public LoginForm() {
        super(LangUtil.getInstance().getMessage(LangUtil.tit_login));
        try {
            exeute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void exeute() throws Exception {
        // Set up this Displayable to listen to command events
        username = new TextField(LangUtil.getInstance().getMessage(LangUtil.tag_account), "", 15, TextField.ANY);
        password = new TextField(LangUtil.getInstance().getMessage(LangUtil.tag_pwd), "", 15, TextField.PASSWORD);
        autoLogin = new ChoiceGroup(LangUtil.getInstance().getMessage(LangUtil.tag_autoLogin), Choice.MULTIPLE,
                new String[]{LangUtil.getInstance().getMessage(LangUtil.tag_rememberMe)}, null);
        this.append(username);
        this.append(password);
        this.append(autoLogin);
        setCommandListener(this);
        // add the Exit command
        loginCom = new Command(LangUtil.getInstance().getMessage(LangUtil.but_login), Command.OK, 1);
        registerCom = new Command(LangUtil.getInstance().getMessage(LangUtil.but_register), Command.OK, 1);
        addCommand(new Command(LangUtil.getInstance().getMessage(LangUtil.but_quit), Command.EXIT, 1));
        addCommand(loginCom);
        addCommand(registerCom);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == loginCom) {
            t = new Thread(this);
            t.start();
        } else if (command == registerCom) {
            RegisterForm register = new RegisterForm();
            WSClientMIDlet.setDisplay(register);
        } else if (command.getCommandType() == Command.EXIT) {
            WSClientMIDlet.quitApp();
        }
    }

    public void run() {
        String _username = username.getString();
        String _password = password.getString();
        boolean auto = autoLogin.isSelected(0);
        Login login = new Login();
        boolean re = login.login(_username, _password,auto);
        if (re) {
            IndexForm index = new IndexForm();
            WSClientMIDlet.setDisplay(index);
        } else {
            StringItem resultItem = new StringItem("处理后的字符串是:", "登录失败");
            this.append(resultItem);
        }
    }
}
